<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>拉货添加</title>
    <% include("/common/common_css_js.html"){} %>
  </head>
  <body>

    <a  class="mini-button" iconCls="icon-ok" onclick="savePullGoodsToDb()">保存</a>

    <div id="pullForm" style="width: 100%;margin-top: 5px">
      <span>主单号:</span>
      <input id="masterBillId" name="masterBillId" emptyText="请输入主单号"  class="mini-textbox" style="width: 200px" required="true" enabled="false" requiredErrorText="主单号不能为空" />
      <input id="masterBillTypeId" name="masterBillTypeId" emptyText="请输入主单类型ID"  class="mini-hidden" value="261" required="true" enabled="false" enabled="false" requiredErrorText="主单类型ID不能为空" />

      <span>目的市:</span>
      <input id="pullDestCityName" name="pullDestCityName" class="mini-textbox" emptyText="目的市名称" required="true" enabled="false"   requiredErrorText="目的市名称不能为空"/>
      <input id="pullDestCityId" name="pullDestCityId" emptyText="请输入目的市"  class="mini-hidden" required="true" enabled="false" enabled="false" requiredErrorText="目的市ID不能为空" />

      <div style="margin-top: 5px"></div>
      <span>包含分单列表:</span>
      <div style="margin-top: 5px"></div>

      <div id="pullGoodsDatagrid" class="mini-datagrid"
           style="width: 100%; height: 200px;" idField="id"
           showPager="false"  allowResize="true"
           allowCellSelect="true">
        <div property="columns">
          <div field="nativeBillId" width="80" headerAlign="center" align="center" >分单号</div>
          <div field="flightName" width="80" headerAlign="center" align="center" >班次名称</div>
          <div field="flightDate" width="80" headerAlign="center" align="center"  dateFormat="yyyy-MM-dd" renderer="ondayRenderer">班次日期</div>
          <div field="loadNumber" width="80" headerAlign="center" align="right">配载件数</div>
          <div field="loadActualWeight" width="80" headerAlign="center" align="right">配载实重</div>
          <div field="loadChargeableWeight" width="80" headerAlign="center" align="right">配载计重</div>
          <div field="loadVolume" width="80" headerAlign="center" align="right">配载体积</div>


          <div field="pullNumber" width="100" headerAlign="center" align="right"><span style="color: red">拉货件数</span></div>
          <div field="pullActualWeight" width="100" headerAlign="center" align="right"><span style="color: red">拉货实重</span></div>
          <div field="pullChargeableWeight" width="100" headerAlign="center" align="right"><span style="color: red">拉货计重</span></div>
          <div field="pullVolume" width="100" headerAlign="center" align="right"><span style="color: red">拉货体积</span></div>
          <div field="pullAmount" width="100" headerAlign="center" align="right"><span style="color: red">拉货调整金额</span></div>
          <div field="pullComment" width="200" headerAlign="center" align="left"><span style="color: red">分单拉货备注</span></div>
          <div field="pullDisposeReasonIds" width="200" headerAlign="center" align="left" allowSort="true"  data-options='{cacheName:"sysDic"}' renderer="commonRenderXtDicCacheNameByIds"><span style="color: red">分单拉货原因</span></div>


          <div field="pullDisposeId" width="80" headerAlign="center" align="center" data-options='{cacheName:"sysDic"}' renderer="commonRender"><span style="color: red">处置方式</span></div>
          <div field="pullDisposeAirportId" width="200" headerAlign="center" align="center" data-options='{cacheName:"airport"}' renderer="commonRender"><span style="color: red">处置始发机场</span></div>
          <div field="pullDisposeAirlinesId" width="200" headerAlign="center" align="center" data-options='{cacheName:"airlines"}' renderer="commonRender"><span style="color: red">处置航空公司</span></div>
          <div field="pullDisposeFlightDate" width="100" headerAlign="center" align="center"  dateFormat="yyyy-MM-dd" renderer="ondayRenderer"><span style="color: red">处置班次日期</span></div>
          <div field="pullDisposeFlightNo" width="0" headerAlign="center" align="center" allowSort="true">处置班次ID</div>
          <div field="pullDisposeFlightName" width="100" headerAlign="center" align="center" allowSort="true"><span style="color: red">处置班次名</span></div>

          <div width="100" headerAlign="center" align="center" renderer="showOpRenderer">操作</div>

        </div>

      </div>

      <div style="margin-top: 5px"></div>
      <span style="color: red;font-weight: bold">
        主单拉货件数、重实、计重、体积填写提示：
        <div style="margin-top: 5px"></div>
        <span style="color: blue;margin-left: 5px">
          1.如果主单下有多个分单，请按该主单拉货的的实际情况分别填写件数、重量、体积信息
        </span>
        <div style="margin-top: 5px"></div>
        <span style="color: blue;margin-left: 5px">
          2.如果主单下只有一个分单，且该分单做【全部拉货补发】或【部分拉货补发】，主单件数、重量、体积信息会全部清零
        </span>
      </span>

      <div style="margin-top: 5px"></div>
      <span>主单拉货件数:</span>
      <input id="pullDisposeNumber" name="pullDisposeNumber" style="width:80px" class="mini-textbox" type="float" value="0" emptyText="主单拉货件数" required="true" requiredErrorText="主单拉货件数不能为空"/>
      <span>主单拉货总实重:</span>
      <input id="pullDisposeActualWeight" name="pullDisposeActualWeight" style="width:80px" class="mini-textbox" type="float" value="0" emptyText="主单拉货总实重" required="true" requiredErrorText="主单拉货总实重不能为空"/>
      <span>主单拉货总计重:</span>
      <input id="pullDisposeChargeableWeight" name="pullDisposeChargeableWeight" style="width:80px" class="mini-textbox" type="float" value="0" emptyText="主单拉货总计重" required="true" requiredErrorText="主单拉货总计重不能为空"/>
      <span>主单拉货总体积:</span>
      <input id="pullDisposeVolume" name="pullDisposeVolume" style="width:80px" class="mini-textbox" type="float" value="0" emptyText="主单拉货总体积" required="true" requiredErrorText="主单拉货总体积不能为空"/>


      <div style="margin-top: 5px"></div>
      <span style="color: blue;font-weight: bold">主单拉货金额调整区域<span style="color: red">(注意：对方退回本公司填写负值，如本公司需要补交填写正值)</span></span>
      <div style="margin-top: 5px;width: 98%;border: 1px solid grey;padding: 2px">
        <span>航空(汽运)运费:</span>
        <input id="pullDisposeTransportCostAmount" name="pullDisposeTransportCostAmount" style="width:80px" class="mini-textbox" type="float" value="0" emptyText="航空运费" required="true" requiredErrorText="航空运费不能为空" onvaluechanged="calcTotalCostAmount"/>
        <span>地面运费:</span>
        <input id="pullDisposeGroundAmount" name="pullDisposeGroundAmount" style="width:80px" class="mini-textbox" type="float" value="0" emptyText="地面运费" required="true" requiredErrorText="地面运费不能为空" onvaluechanged="calcTotalCostAmount"/>
        <span>航空附加费:</span>
        <input id="pullDisposeAirlinesAddCostAmount" name="pullDisposeAirlinesAddCostAmount" style="width:80px" class="mini-textbox" type="float" value="0" emptyText="航空附加费" required="true" requiredErrorText="航空附加费不能为空" onvaluechanged="calcTotalCostAmount"/>
        <span>其他费用:</span>
        <input id="pullDisposeOtherChargesAmount" name="pullDisposeOtherChargesAmount" style="width:80px" class="mini-textbox" type="float" value="0" emptyText="其他费用" required="true" requiredErrorText="其他费用不能为空" onvaluechanged="calcTotalCostAmount"/>
        <div style="margin-top: 5px"></div>
        <span>总调整金额:</span>
        <input id="pullDisposeAmount" name="pullDisposeAmount" style="width:80px" class="mini-textbox" type="float" value="0" emptyText="总调整金额" required="true" requiredErrorText="总调整金额不能为空" enabled="false"/>

        <span style="color:red;font-weight: bold">主单拉货调整金额=主单拉货总计重*原拉货重量费率(自行手动计算完成)</span>

      </div>


      <div style="margin-top: 5px"></div>
      <span>主单拉货备注:</span>
      <textarea id="pullDisposeComment" name="pullDisposeComment" class="mini-textarea" style="width: 80%;height: 50px" emptyText="请输入主单拉货备注信息"  required="true"></textarea>


      <div style="margin-top: 5px"></div>
      <a  class="mini-button" iconCls="icon-ok" onclick="generatePullDisposeComment()">生成主单拉货备注</a>

    </div>


    <!-- 拉货编辑窗口 -->
    <div id="pullGoodsPanel" class="mini-window" title="拉货处置" style="width:1200px;height:450px;"
         showMaxButton="true" showCollapseButton="false" showMinButton="true" showShadow="true"
         showToolbar="true" showFooter="true" showModal="true" allowResize="true" allowDrag="true">
      <div property="footer" style="padding:10px;text-align: right">
        <a  class="mini-button" iconCls="icon-ok" onclick="savePullGoods()">保存</a>
        <a  class="mini-button" iconCls="icon-close" onclick="public_onCancel('pullGoodsPanel')">关闭</a>
      </div>

      <input id="loadBillId" name="loadBillId" emptyText="配载记录ID"  class="mini-hidden" required="true" enabled="false" requiredErrorText="配载记录ID不能为空" />

      <div style="margin-top: 5px;margin-bottom: 5px">分单原始配载信息</div>
      <span>分单号:</span><input id="nativeBillId" name="nativeBillId" emptyText="请输入分单号"  class="mini-textbox" required="true" enabled="false" requiredErrorText="分单号不能为空" />
      <span>班次名称:</span><input id="flightName" name="flightName" emptyText="请输入班次名称"  class="mini-textbox" required="true" enabled="false" requiredErrorText="班次名称不能为空" />
      <span>班次日期:</span><input id="flightDate" name="flightDate" emptyText="请输入班次日期"  class="mini-textbox" required="true" enabled="false" requiredErrorText="班次日期不能为空" />
      <div style="margin-top: 5px;"></div>
      <span>配载件数:</span><input id="loadNumber" name="loadNumber" emptyText="请输入配载件数"  class="mini-textbox" required="true" enabled="false" requiredErrorText="配载件数不能为空" />
      <span>配载实重:</span><input id="loadActualWeight" name="loadActualWeight" emptyText="请输入配载实重"  class="mini-textbox" required="true" enabled="false" requiredErrorText="配载实重不能为空" />
      <span>配载计重:</span><input id="loadChargeableWeight" name="loadChargeableWeight" emptyText="请输入配载计重"  class="mini-textbox" required="true" enabled="false" requiredErrorText="配载计重不能为空" />
      <span>配载体积:</span><input id="loadVolume" name="loadVolume" emptyText="请输入配载体积"  class="mini-textbox" required="true" enabled="false" requiredErrorText="配载体积不能为空" />

      <div style="margin-top: 5px;margin-bottom: 5px;color: red">分单拉货信息</div>
      <span>拉货原因:</span>
      <input id="pullDisposeReasonIds" name="pullDisposeReasonIds" class="mini-combobox" style="width:800px;" textField="name" valueField="id" emptyText="请选择..." required="true"
             allowInput="false" showNullItem="true" nullItemText="请选择..." requiredErrorText="没有选择拉货原因" multiSelect="true" />
      <div style="margin-top: 5px"></div>
      <span>拉货件数:</span><input id="pullNumber" name="pullNumber" emptyText="请输入拉货件数" value="0" class="mini-textbox" vtype="int" required="true" requiredErrorText="拉货件数不能为空" />
      <span>拉货实重:</span><input id="pullActualWeight" name="pullActualWeight" emptyText="请输入拉货实重" value="0"  class="mini-textbox" vtype="float" required="true" requiredErrorText="拉货实重不能为空" />
      <span>拉货计重:</span><input id="pullChargeableWeight" name="pullChargeableWeight" emptyText="请输入拉货计重"  value="0" class="mini-textbox" vtype="float" required="true" requiredErrorText="拉货计重不能为空" />
      <span>拉货体积:</span><input id="pullVolume" name="pullVolume" emptyText="请输入拉货体积"  class="mini-textbox" value="0" vtype="float" required="true" requiredErrorText="拉货体积不能为空" />
      <div style="margin-top: 5px;margin-bottom: 5px;font-weight: bold;color: red">
        <div>关于调整金的填写方法：
          <span>1.如果是补收客户的费用请填写正值</span>
          <span style="margin-left: 10px">2.如果是退费给客户请填写负值</span>
          <span style="margin-left: 10px;font-weight: bold">3.该调整金额包括分单上一切需要进行调整的费用总和，如分单业务费、分单其他费用等等</span>
        </div>
      </div>
      <span>拉货调整金额:</span><input id="pullAmount" name="pullAmount" emptyText="请输入拉货调整金额"  class="mini-textbox" value="0" vtype="float" required="true" requiredErrorText="拉货调整金额不能为空" />

      <div style="margin-top: 5px;margin-bottom: 5px;color: red">分单拉货处置信息</div>
      <span>处置方式:</span>
      <input id="pullDisposeId" name="pullDisposeId" class="mini-combobox" style="width:150px;" textField="name" valueField="id" emptyText="请选择..." required="true"
             allowInput="false" showNullItem="true" nullItemText="请选择..." requiredErrorText="没有选择拉货处置方式" onvaluechanged="pullDisposeIdChanged"/>


      <span id="spanAirportTitle">始发机场:</span>
      <div id="pullDisposeAirportId" name="pullDisposeAirportId" class="mini-combobox" style="width:150px;"  popupWidth="1000" textField="name" valueField="id"
           required="true" requiredErrorText="没有选择始发机场"
           allowInput="true"
           emptyText="选择始发机场">
        <div property="columns">
          <div header="ID" field="id" width="30"></div>
          <div header="机场名称" field="name"></div>
          <div header="简称" field="shortName"></div>
        </div>
      </div>

      <span id="spanAirlinesTitle">航空公司:</span>
      <div id="pullDisposeAirlinesId" name="pullDisposeAirlinesId"
           class="mini-combobox"
           style="width:250px;"
           popupWidth="600" textField="name" valueField="id"
           allowInput="true" required="true" requiredErrorText="没有选择航空公司"
           emptyText="选择航空公司"
           onvaluechanged="pullFlightLoad">
        <div property="columns">
          <div header="ID" field="id" width="30"></div>
          <div header="名称" field="name"></div>
          <div header="简称" field="shortName"></div>
          <div header="两字码" field="twoCode"></div>
        </div>
      </div>

      <span>班次日期:</span>
      <input id="pullDisposeFlightDate" name="pullDisposeFlightDate" class="mini-datepicker" style="width:150px;"  nullValue="null" required="true" requiredErrorText="没有设置航班日期"
             emptyText="班次日期"
             format="yyyy-MM-dd"  showOkButton="false" showClearButton="true" onvaluechanged="pullFlightLoad"/>

      <span>班次号:</span>
      <div id="pullDisposeFlightNo" name="pullDisposeFlightNo" class="mini-combobox" style="width:150px;"  popupWidth="400" textField="name" valueField="id"
           required="true" requiredErrorText="没有选择航班号"
           emptyText="选择航班号"
           multiSelect="false"
           onvaluechanged="pullFlightNoChanged">
        <div property="columns">
          <div header="ID" field="id"></div>
          <div header="班次号" field="name"></div>
          <div header="起飞时间" field="planTakeOffTime"></div>
          <div header="航空公司简称" field="shortName"></div>
        </div>
      </div>
      <!-- 隐藏字段 -->
      <input id="pullDisposeFlightName" name="pullDisposeFlightName" class="mini-textbox" emptyText="班次名称" required="true"   requiredErrorText="班次名称不能为空"/>
    </div>



    <script type="text/javascript">
      mini.parse();

      let masterBillId = mini.get("masterBillId");
      let masterBillTypeId = mini.get("masterBillTypeId");

      let pullDestCityName = mini.get("pullDestCityName");
      let pullDestCityId = mini.get("pullDestCityId");


      let pullGoodsDatagrid = mini.get("pullGoodsDatagrid");

      let pullDisposeNumber = mini.get("pullDisposeNumber");
      let pullDisposeActualWeight = mini.get("pullDisposeActualWeight");
      let pullDisposeChargeableWeight = mini.get("pullDisposeChargeableWeight");
      let pullDisposeVolume = mini.get("pullDisposeVolume");

      let pullDisposeTransportCostAmount = mini.get("pullDisposeTransportCostAmount");
      let pullDisposeGroundAmount = mini.get("pullDisposeGroundAmount");
      let pullDisposeAirlinesAddCostAmount = mini.get("pullDisposeAirlinesAddCostAmount");
      let pullDisposeOtherChargesAmount = mini.get("pullDisposeOtherChargesAmount");
      let pullDisposeAmount = mini.get("pullDisposeAmount");
      let pullDisposeComment = mini.get("pullDisposeComment");


      let pullLoadArray = [];

      let pullOriginalLoadArray = [];

      let masterBillPojoJsonObject;

      let loadBillId = mini.get("loadBillId");


      let nativeBillId = mini.get("nativeBillId");
      let flightName = mini.get("flightName");
      let flightDate = mini.get("flightDate");
      let loadNumber = mini.get("loadNumber");
      let loadActualWeight = mini.get("loadActualWeight");
      let loadChargeableWeight = mini.get("loadChargeableWeight");
      let loadVolume = mini.get("loadVolume");

      let pullDisposeReasonIds = mini.get("pullDisposeReasonIds");
      let pullNumber = mini.get("pullNumber");
      let pullActualWeight = mini.get("pullActualWeight");
      let pullChargeableWeight = mini.get("pullChargeableWeight");
      let pullVolume = mini.get("pullVolume");
      let pullAmount = mini.get("pullAmount");

      let pullDisposeId = mini.get("pullDisposeId");
      let pullDisposeAirportId = mini.get("pullDisposeAirportId");
      let pullDisposeAirlinesId = mini.get("pullDisposeAirlinesId");
      let pullDisposeFlightDate = mini.get("pullDisposeFlightDate");
      let pullDisposeFlightNo = mini.get("pullDisposeFlightNo");
      let pullDisposeFlightName = mini.get("pullDisposeFlightName");

      // 主单类型
      let masterBillType = {flight:261, auto:262};

      $(function (){

        masterBillId.setValue("${masterBillId!}");
        masterBillTypeId.setValue("${masterBillTypeId!}");

        masterBillPojoJsonObject = JSON.parse('${masterBillPojo}');
        pullDestCityId.setValue(masterBillPojoJsonObject.destCityId);

        let destCityName = commonRendderNameByKeyAndId("city",masterBillPojoJsonObject.destCityId);
        pullDestCityName.setValue(destCityName);

        let loadPojoListJsonArray = JSON.parse('${loadPojoList}');

        // 迭代加入对象
        for(let loadObj of loadPojoListJsonArray){
          let addObj = {};
          addObj.id = loadObj.id;
          addObj.masterBillId = loadObj.masterBillId;
          addObj.masterBillTypeId = loadObj.masterBillTypeId;
          addObj.nativeBillId = loadObj.nativeBillId;
          addObj.airlinesId = loadObj.airlinesId;
          addObj.flightNo = loadObj.flightNo;
          addObj.flightName = loadObj.flightName;
          addObj.flightDate = loadObj.flightDate;
          addObj.loadNumber = loadObj.loadNumber;
          addObj.loadActualWeight = loadObj.loadActualWeight;
          addObj.loadChargeableWeight = loadObj.loadChargeableWeight;
          addObj.loadVolume = loadObj.loadVolume;
          addObj.pullDisposeReasonIds = undefined;
          addObj.shareTransportCostAmount = loadObj.shareTransportCostAmount;
          addObj.shareGroundAmount = loadObj.shareGroundAmount;
          addObj.shareAirlinesAddCostAmount = loadObj.shareAirlinesAddCostAmount;
          addObj.shareTotalCostAmount = loadObj.shareTotalCostAmount;
          addObj.shareTotalCostComment = loadObj.shareTotalCostComment;

          addObj.pullDisposeId = undefined;
          addObj.pullDisposeAirportId = undefined;
          addObj.pullDisposeAirlinesId = undefined;
          addObj.pullDisposeFlightDate = undefined;
          addObj.pullDisposeFlightNo = undefined;
          addObj.pullDisposeFlightName = undefined;

          addObj.pullNumber = 0;
          addObj.pullActualWeight = 0;
          addObj.pullChargeableWeight = 0;
          addObj.pullVolume = 0;
          addObj.pullAmount = 0;
          addObj.pullComment = "";

          // 添加到数组中
          pullLoadArray.push(addObj);

          // 添加到 datagrid
          pullGoodsDatagrid.addRow(addObj);

        }


        // 拉货处置方式
        let pullDisposeIdJsonData = commonRenderSysDicDropListByKey("sysDic","XT_DIC_NATIVE_PULL_DISPOSE");
        pullDisposeId.load(pullDisposeIdJsonData);

        // 拉货原因
        let pullDisposeReasonIdJsonData = commonRenderSysDicDropListByKey("sysDic","XT_DIC_DISPOSE_REASON");
        pullDisposeReasonIds.load(pullDisposeReasonIdJsonData);

        // 获取主单类型，决定页面上的控件是否显示
        let masterBillTypeIdValue = masterBillTypeId.getValue();
        if(masterBillTypeIdValue == masterBillType.flight){
          // 航空公司两字码
          let pullAirlinesIdJsonData = commonRenderDropList("airlines");
          pullDisposeAirlinesId.load(pullAirlinesIdJsonData);

          // 始发机场下拉列表
          let pullAirportIdJsonData = commonRenderDropList("airport");
          pullDisposeAirportId.load(pullAirportIdJsonData);

          // 隐藏处置班次名称
          pullDisposeFlightName.hide();
        }
        else{
          // 汽运的，需要隐藏
          pullDisposeAirlinesId.hide();
          pullDisposeAirportId.hide();
          pullDisposeFlightNo.hide();

          // 同时把隐藏的控件设置为非要求的
          pullDisposeAirlinesId.set({required:false});
          pullDisposeAirportId.set({required:false});
          pullDisposeFlightNo.set({required:false});

          $("#spanAirportTitle").hide();
          $("#spanAirlinesTitle").hide();

        }
      });


      /**
       * 分单列表拉货操作按钮显示
       * @param e
       * @returns {string}
       */
      function showOpRenderer(e){
        let loadBillIdValue = e.record.id;
        // 很重要，必须记录
        loadBillId.setValue(loadBillIdValue);

        return '<a class="mini-button" onclick="showBillPullProcess(\''+ loadBillIdValue +'\')" style="padding: 2px">分单拉货</a>';
      }

      function showBillPullProcess(loadBillId){

        // 直接禁止修改
        pullDisposeNumber.setEnabled(true);
        pullDisposeActualWeight.setEnabled(true);
        pullDisposeChargeableWeight.setEnabled(true);
        pullDisposeVolume.setEnabled(true);

        // 根据id到数组中找到对应的对象
        for(let obj of pullLoadArray){
          if(obj.id == loadBillId){

            nativeBillId.setValue(obj.nativeBillId);
            flightName.setValue(obj.flightName);
            flightDate.setValue(public_cstDataConvert(obj.flightDate));
            loadNumber.setValue(obj.loadNumber);
            loadActualWeight.setValue(obj.loadActualWeight);
            loadChargeableWeight.setValue(obj.loadChargeableWeight);
            loadVolume.setValue(obj.loadVolume);

            pullDisposeReasonIds.setValue(obj.pullDisposeReasonIds);
            pullNumber.setValue(obj.pullNumber);
            pullActualWeight.setValue(obj.pullActualWeight);
            pullChargeableWeight.setValue(obj.pullChargeableWeight);
            pullVolume.setValue(obj.pullVolume);
            pullAmount.setValue(obj.pullAmount);

            pullDisposeId.setValue(obj.pullDisposeId);
            pullDisposeAirportId.setValue(obj.pullDisposeAirportId);
            pullDisposeAirlinesId.setValue(obj.pullDisposeAirlinesId);
            pullDisposeFlightDate.setValue(obj.pullDisposeFlightDate);
            pullDisposeFlightNo.setValue(obj.pullDisposeFlightNo);
            pullDisposeFlightName.setValue(obj.pullDisposeFlightName);

            public_showModelAtPos("pullGoodsPanel", "center", "middle");
            break;
          }
        }
      }

      // 保存到主页面
      function savePullGoods(){
        // 1.先做校验
        // 1.1 拉货原因
        let pullDisposeReasonIdsValue = pullDisposeReasonIds.getValue();
        if(public_isEmpty(pullDisposeReasonIdsValue)){
          showAlert("没有选择拉货原因!");
          return;
        }

        let pullDisposeReasonIdsText = pullDisposeReasonIds.getText();

        // loadNumber loadActualWeight  loadChargeableWeight  loadVolume
        let loadNumberValue = loadNumber.getValue();
        let loadActualWeightValue = loadActualWeight.getValue();
        let loadChargeableWeightValue = loadChargeableWeight.getValue();
        let loadVolumeValue = loadVolume.getValue();

        // 1.2 拉货件数
        let pullNumberValue = pullNumber.getValue();
        if(!public_isNumber(pullNumberValue)){
          showAlert("拉货件数不是数字!");
          return;
        }

        pullNumberValue = public_toNumber(pullNumberValue,-1);
        if(pullNumberValue <= 0){
          showAlert("拉货件数不能小于或等于零!");
          return;
        }

        if(pullNumberValue>loadNumberValue){
          showAlert("拉货件数不能大于配载件数!");
          return;
        }

        // 1.3 拉货实重
        let pullActualWeightValue = pullActualWeight.getValue();
        if(!public_isNumber(pullActualWeightValue)){
          showAlert("拉货实重不是数字!");
          return;
        }
        pullActualWeightValue = public_toNumber(pullActualWeightValue,-1);
        if(pullActualWeightValue < 0){
          showAlert("拉货实重不能小于零!");
          return;
        }

        if(pullActualWeightValue>loadActualWeightValue){
          showAlert("拉货实重不能大于配载实重!");
          return;
        }

        // 1.4 拉货计重
        let pullChargeableWeightValue = pullChargeableWeight.getValue();
        if(!public_isNumber(pullActualWeightValue)){
          showAlert("拉货计重不是数字!");
          return;
        }
        pullChargeableWeightValue = public_toNumber(pullChargeableWeightValue,-1);
        if(pullChargeableWeightValue <= 0){
          showAlert("拉货计重不能小于或等于零!");
          return;
        }

        if(pullChargeableWeightValue>loadChargeableWeightValue){
          showAlert("拉货计重不能大于配载计重!");
          return;
        }

        // 1.5 拉货体积
        let pullVolumeValue = pullVolume.getValue();
        if(!public_isNumber(pullVolumeValue)){
          showAlert("拉货休积不是数字!");
          return;
        }
        pullVolumeValue = public_toNumber(pullVolumeValue,-1);
        if(pullVolumeValue < 0){
          showAlert("拉货休积不能小于零!");
          return;
        }

        if(pullVolumeValue>loadVolumeValue){
          showAlert("拉货体积不能大于配载体积!");
          return;
        }

        // 1.6 拉货调整金额
        let pullAmountValue = public_toNumber(pullAmount.getValue(),-1);
        if(!public_isNumber(pullAmountValue)){
          showAlert("拉货调整金额不是数字!");
          return;
        }

        // 1.7 处置方式
        let pullDisposeIdValue = pullDisposeId.getValue();
        if(public_isEmpty(pullDisposeIdValue)){
          showAlert("没有选择拉货处置方式!");
          return;
        }

        /**
         * SUPPLY(231, "补发", "补发"),
         RELOAD(232, "改配", "改配"),
         REFUND(233, "退客户", "退客户");
         */
        let pullDisposeType = {supply:231, reload:232, refund:233};

        let pullDisposeAirportIdValue = pullDisposeAirportId.getValue();
        let pullDisposeAirlinesIdValue = pullDisposeAirlinesId.getValue();
        let pullDisposeFlightDateValue = pullDisposeFlightDate.getValue();
        let pullDisposeFlightNoValue = pullDisposeFlightNo.getValue();
        let pullDisposeFlightNameValue = pullDisposeFlightName.getValue();

        if(masterBillTypeId.getValue() == masterBillType.flight){

          if(pullDisposeIdValue != pullDisposeType.refund){
            // 不是退货，则需要校验始发机场，
            if(public_isEmpty(pullDisposeAirportIdValue)){
              showAlert("没有选择拉货始发机场!");
              return;
            }

            if(public_isEmpty(pullDisposeAirlinesIdValue)){
              showAlert("没有选择拉货航空公司!");
              return;
            }

            if(public_isEmpty(pullDisposeFlightDateValue)){
              showAlert("没有选择拉货班次日期!");
              return;
            }

            if(public_isEmpty(pullDisposeFlightNoValue)){
              showAlert("没有选择拉货班次号!");
              return;
            }
          }
        }
        else{
          if(pullDisposeIdValue != pullDisposeType.refund){
            // 不是退货，则需要校验始发机场，

            if(public_isEmpty(pullDisposeFlightDateValue)){
              showAlert("没有选择拉货班次日期!");
              return;
            }

            if(public_isEmpty(pullDisposeFlightNameValue)){
              showAlert("没有填写拉货班次号!");
              return;
            }
          }
        }


        // 判断处置是不是同一个日期，同一个航班，如果是，则不允许保存
        let flightNameValue = flightName.getValue();
        let flightDateValue = flightDate.getValue();

        if(flightNameValue == pullDisposeFlightNameValue && flightDateValue == public_cstDataConvert(pullDisposeFlightDateValue)){
          showAlert("分单拉货处置的班次名称与班次日期与原班次名称和班次日期相同!");
          return;
        }


        let loadBillIdValue = loadBillId.getValue();

        let totalPullNumber = 0;
        let totalPullActualWeight = 0;
        let totalPullChargeableWeight = 0;
        let totalPullVolume = 0;

        // 2.获取拉货的分单填写信息
        for(let obj of pullLoadArray){
          if(obj.id == loadBillIdValue){
            // 开始修改其设定的值
            obj.pullDisposeReasonIds = pullDisposeReasonIdsValue;

            obj.pullDisposeId = pullDisposeIdValue;
            obj.pullDisposeAirportId = pullDisposeAirportIdValue;
            obj.pullDisposeAirlinesId = pullDisposeAirlinesIdValue;
            obj.pullDisposeFlightDate = pullDisposeFlightDateValue;
            obj.pullDisposeFlightNo = pullDisposeFlightNoValue;
            obj.pullDisposeFlightName = pullDisposeFlightNameValue;

            obj.pullNumber = pullNumberValue;
            obj.pullActualWeight = pullActualWeightValue;
            obj.pullChargeableWeight = pullChargeableWeightValue;
            obj.pullVolume = pullVolumeValue;
            obj.pullAmount = pullAmountValue;
            let pullCommentValue = "【分单】" + nativeBillId.getValue() + ",【拉货】 件数:" + pullNumberValue + ",计重:"+  pullChargeableWeightValue;
            pullCommentValue += ",【原因】" + pullDisposeReasonIdsText;
            pullCommentValue += ",【处置】方式:" + commonRendderNameByKeyAndId("sysDic",pullDisposeIdValue);
            if(masterBillTypeId.getValue() == masterBillType.flight){
              if(pullDisposeIdValue != pullDisposeType.refund){
                pullCommentValue += ", 始发机场:" + commonRendderNameByKeyAndId("airport",pullDisposeAirportIdValue);
                pullCommentValue += ", 航空公司:" + commonRendderNameByKeyAndId("airlines",pullDisposeAirlinesIdValue);
                pullCommentValue += ", 班次日期:" + public_cstDataConvert(pullDisposeFlightDateValue);
                pullCommentValue += ", 班次号:" + pullDisposeFlightNameValue;
              }
            }
            else{
              if(pullDisposeIdValue != pullDisposeType.refund){
                pullCommentValue += ", 班次日期:" + public_cstDataConvert(pullDisposeFlightDateValue);
                pullCommentValue += ", 班次号:" + pullDisposeFlightNameValue;
              }
            }

            obj.pullComment = pullCommentValue;
          }

          totalPullNumber += obj.pullNumber;
          totalPullActualWeight += obj.pullActualWeight;
          totalPullChargeableWeight += obj.pullChargeableWeight;
          totalPullVolume += obj.pullVolume;
        }

        // 设置主页面的值
        // pullDisposeNumber  pullDisposeActualWeight  pullDisposeChargeableWeight  pullDisposeVolume
        pullDisposeNumber.setValue(totalPullNumber);
        pullDisposeActualWeight.setValue(totalPullActualWeight);
        pullDisposeChargeableWeight.setValue(totalPullChargeableWeight);
        pullDisposeVolume.setValue(totalPullVolume);
        // totalPullNumber == masterBillPojoJsonObject.totalNumber &&
        // 如果只有一个，且是补发，则直接清零，因为主单不需要调整
        if(pullLoadArray.length==1 && pullLoadArray[0].pullDisposeId == pullDisposeType.supply) {
          // 这是全拉补发全部清零
          pullDisposeNumber.setValue(0);
          pullDisposeActualWeight.setValue(0);
          pullDisposeChargeableWeight.setValue(0);
          pullDisposeVolume.setValue(0);
          // 直接禁止修改
          pullDisposeNumber.setEnabled(false);
          pullDisposeActualWeight.setEnabled(false);
          pullDisposeChargeableWeight.setEnabled(false);
          pullDisposeVolume.setEnabled(false);
        }


        // 清空 grid
        pullGoodsDatagrid.clearRows();

        for(let obj of pullLoadArray){
          pullGoodsDatagrid.addRow(obj);
        }

        // 关闭面板
        public_onCancel('pullGoodsPanel');

      }

      /**
       * 清空拉货处置信息
       */
      function clearPull(){
        pullDisposeAirportId.setValue();
        pullDisposeAirlinesId.setValue();
        pullDisposeFlightDate.setValue();
        pullDisposeFlightNo.setValue();
        pullDisposeFlightNo.load();
        pullDisposeFlightName.setValue();
      }

      /**
       * 设置拉货处置信息状态
       */
      function setPullLockStatus(boolValue){
        pullDisposeAirportId.setEnabled(boolValue);
        pullDisposeAirlinesId.setEnabled(boolValue);
        pullDisposeFlightDate.setEnabled(boolValue);
        pullDisposeFlightNo.setEnabled(boolValue);
        pullDisposeFlightName.setEnabled(boolValue);
      }

      /**
       * SUPPLY(231, "补发", "补发"),
       RELOAD(232, "改配", "改配"),
       REFUND(233, "退客户", "退客户");
       */

      let pullDispose = {supply:231, reload:232, refund:233};

      function pullDisposeIdChanged(e){
        clearPull();
        let pullDisposeIdValue = e.selected;
        if(public_isEmpty(pullDisposeIdValue)){
          // 没有选择，请空所有选择
          return;
        }

        if(pullDisposeIdValue.id == pullDispose.refund){
          // 这是退客户，则清空并锁定
          setPullLockStatus(false);
        }
        else{
          let masterBillTypeIdValue = masterBillTypeId.getValue();
          if(masterBillTypeIdValue == masterBillType.flight){
            // 恢复主单上的默认值
            pullDisposeAirportId.setValue(masterBillPojoJsonObject.airportId);
            pullDisposeAirlinesId.setValue(masterBillPojoJsonObject.airlinesId);
          }
          setPullLockStatus(true);
        }

      }

      function pullFlightLoad(){
        if(masterBillTypeId.getValue() == masterBillType.flight){
          if(isEmptyNoAlert(pullDisposeAirportId)){
            return;
          }

          if(isEmptyNoAlert(pullDisposeAirlinesId)){
            return;
          }

          if(isEmptyNoAlert(pullDisposeFlightDate)){
            return;
          }

          let destCityIdValue = pullDestCityId.getValue();
          let airportIdValue = pullDisposeAirportId.getValue();
          let airlinesIdValue = pullDisposeAirlinesId.getValue();
          let flightDateValue = pullDisposeFlightDate.getFormValue();

          // 加载后直接返回List
          let ret = public_getServerData("${base}/pages/flight_manage/queryFlightNoListByDropCnd",
                  {
                    cityId : destCityIdValue,
                    airportId : airportIdValue,
                    airlinesId : airlinesIdValue,
                    flightDate : flightDateValue
                  });
          // 绑定数据
          pullDisposeFlightNo.load(ret);
          // 清空一下航班名称
          pullDisposeFlightName.setValue();
        }

      }

      function pullFlightNoChanged(e){
        pullDisposeFlightName.setValue(e.selected.name);
      }

      /**
       * 计算总调整金额
       */
      function calcTotalCostAmount(){
        let pullDisposeTransportCostAmountValue = public_toNumber(pullDisposeTransportCostAmount.getValue(),0);
        let pullDisposeGroundAmountValue = public_toNumber(pullDisposeGroundAmount.getValue(),0);
        let pullDisposeAirlinesAddCostAmountValue = public_toNumber(pullDisposeAirlinesAddCostAmount.getValue(),0);
        let pullDisposeOtherChargesAmountValue = public_toNumber(pullDisposeOtherChargesAmount.getValue(),0);
        pullDisposeAmount.setValue(public_toNumber(pullDisposeTransportCostAmountValue+pullDisposeGroundAmountValue +pullDisposeAirlinesAddCostAmountValue + pullDisposeOtherChargesAmountValue,0));
      }

      /**
       * 生成主单拉货备注
       */
      function generatePullDisposeComment(){
        let pullDisposeNumberValue = pullDisposeNumber.getValue();
        let pullDisposeActualWeightValue = pullDisposeActualWeight.getValue();
        let pullDisposeChargeableWeightValue = pullDisposeChargeableWeight.getValue();
        let pullDisposeVolumeValue = pullDisposeVolume.getValue();

        let pullDisposeTransportCostAmountValue = pullDisposeTransportCostAmount.getValue();
        let pullDisposeGroundAmountValue = pullDisposeGroundAmount.getValue();
        let pullDisposeAirlinesAddCostAmountValue = pullDisposeAirlinesAddCostAmount.getValue();
        let pullDisposeOtherChargesAmountValue = pullDisposeOtherChargesAmount.getValue();
        let pullDisposeAmountValue = pullDisposeAmount.getValue();

        let loadBillComment = "";
        for(let obj of pullLoadArray){
          loadBillComment += "{" + obj.pullComment + "}";
        }


        let masterBillComment = "";
        masterBillComment += "【主单拉货】" + "件数:" + pullDisposeNumberValue;
        masterBillComment += ", 实重:" + pullDisposeActualWeightValue;
        masterBillComment += ", 计重:" + pullDisposeChargeableWeightValue;
        masterBillComment += ", 体积:" + pullDisposeVolumeValue;

        masterBillComment += ", 【主单拉货调整金额】运费:" + pullDisposeTransportCostAmountValue;
        masterBillComment += ", 地面操作费:" + pullDisposeGroundAmountValue;
        masterBillComment += ", 航空附加费:" + pullDisposeAirlinesAddCostAmountValue;
        masterBillComment += ", 其他费用:" + pullDisposeOtherChargesAmountValue;
        masterBillComment += ", 总调整金额:" + pullDisposeAmountValue;

        pullDisposeComment.setValue(masterBillComment + ", 【分单拉货备注】" + loadBillComment.trim());

      }


      /**
       * 保存拉货信息到数据库,并核算成本
       */
      function savePullGoodsToDb(){
        // 校验分单拉货，防止没有做任何拉货直接保存
        let checkResult = false;
        // 件数检验
        for(let obj of pullLoadArray){
          if(obj.pullNumber>0){
            checkResult = true;
          }
        }

        if(!checkResult){
          showAlert("拉货分单中所有待拉货件数都为零，不能做拉货保存!");
          return;
        }

        // 计重检验
        for(let obj of pullLoadArray){
          if(obj.pullChargeableWeight>0){
            checkResult = true;
          }
        }

        if(!checkResult){
          showAlert("拉货分单中所有待拉货计重都为零，不能做拉货保存!");
          return;
        }

        // 处置方式检验
        for(let obj of pullLoadArray){
          if(!public_isEmpty(obj.pullDisposeId)){
            checkResult = true;
          }
        }

        if(!checkResult){
          showAlert("拉货分单中所有待拉货处置方式都为空，不能做拉货保存!");
          return;
        }

        // 拉货主单信息检验
        let pullForm = new mini.Form("#pullForm");

        pullForm.validate();
        if (pullForm.isValid() == false) {
          return;
        }

        let pullFormData = pullForm.getData(true);

        let pullList = [];

        // pullOriginalLoadArray

        for(let obj of pullLoadArray){
          let pullDetailPojo = {};
          pullDetailPojo.nativeBillId = obj.nativeBillId;
          pullDetailPojo.pullNumber = obj.pullNumber;
          pullDetailPojo.pullActualWeight = obj.pullActualWeight;
          pullDetailPojo.pullChargeableWeight = obj.pullChargeableWeight;
          pullDetailPojo.pullVolume = obj.pullVolume;
          pullDetailPojo.pullAmount = obj.pullAmount;
          pullDetailPojo.pullComment = obj.pullComment;
          pullDetailPojo.pullDisposeReasonIds = obj.pullDisposeReasonIds;
          pullDetailPojo.pullDisposeId = obj.pullDisposeId;
          pullDetailPojo.pullDisposeAirportId = obj.pullDisposeAirportId;
          pullDetailPojo.pullDisposeAirlinesId = obj.pullDisposeAirlinesId;
          pullDetailPojo.pullDisposeFlightDate = obj.pullDisposeFlightDate;
          pullDetailPojo.pullDisposeFlightNo = obj.pullDisposeFlightNo;
          pullDetailPojo.pullDisposeFlightName = obj.pullDisposeFlightName;
          pullDetailPojo.associationLoadId = obj.id; // b_native_bill_load表被拉货对象 很重要，需要根据这个 id 找到配载表中的原始对象
          pullList.push(pullDetailPojo);
        }

        pullFormData.pullList = pullList;

        mini.confirm("确定保存拉货信息？", "拉货提示",
            function (action) {
              if (action == "ok") {
                submitPullData(pullFormData);
              }
            }
        );
      }


      /**
       * 提交拉货信息
       */
      function submitPullData(pullDataObject){
        let pullUrl = "${base}/pages/master_bill_manage/pull/addPullData";
        let ret = public_getServerData(pullUrl, pullDataObject);
        showAlert(ret.message,function (){
          if(ret.ok){
            // 关闭主单拉货窗口
            parent.window.closeActiveTab();
          }
        });

      }

    </script>


  </body>
</html>